

package com.example.account;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentTransaction;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.account.Adapter.MainLsAdapter;
import com.example.account.DataBase.AccountData;
import com.example.account.DataBase.AccountDataBase;
import com.example.account.DataBase.AccountDataDao;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    String s[] = {"一月", "二月", "三月", "四月"};
    TextView tv_month_in, tv_month_out, tv_day_in, tv_day_out, tv_month_ye;
    ListView today_ls;
    MainLsAdapter mainLsAdapter;

    private AccountDataBase accountDataBase;
    private AccountDataDao accountDataDao;
    private List<AccountData> lst = new ArrayList<>();
    private List<AccountData> today = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        accountDataBase = AccountDataBase.getDatabase(this);
        accountDataDao = accountDataBase.getDataDao();
        initviews();
        loadData();
        setOn();

        // 添加底部导航栏
        addBottomNavigation();
    }

    private void addBottomNavigation() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.replace(R.id.bottom_navigation_container, new BottomNavigationFragment());
        transaction.commit();
    }

    private void setOn() {
        today_ls.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
                final AccountData now = today.get(i);
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("删除记录");
                builder.setMessage("确定要删除这条记录吗？");
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        accountDataDao.DeleteById(now.getId());
                        loadData();
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        dialog.dismiss();
                    }
                });
                builder.create().show();
                return true;
            }
        });
    }

    private void loadData() {
        List<AccountData> lst = accountDataDao.findAll();
        today.clear();
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String nowtime = simpleDateFormat.format(date);
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH) + 1;
        int day = calendar.get(Calendar.DAY_OF_MONTH);

        // 收入1 支出0
        float in_day = accountDataDao.qaskMoney(1, year, month, day);
        float out_day = accountDataDao.qaskMoney(0, year, month, day);
        float in_sum = accountDataDao.query_Money_by_month_type(1, month, year); // 确保传入当前月份
        float out_sum = accountDataDao.query_Money_by_month_type(0, month, year); // 确保传入当前月份
        // 计算本月余额
        float balance = in_sum - out_sum;
        today = accountDataDao.qask_by_ymd(day, month, year);
        mainLsAdapter = new MainLsAdapter(this, today);
        today_ls.setAdapter(mainLsAdapter);
        for (AccountData tt : today) System.out.println(tt);

        tv_day_in.setText("+￥" + in_day);
        tv_day_out.setText("-￥" + out_day);
        tv_month_in.setText("￥" + in_sum);
        tv_month_out.setText("￥" + out_sum);
        tv_month_ye.setText("￥" + balance);
    }

    @Override
    protected void onStart() {
        super.onStart();
        loadData();
    }

    private void initviews() {
        tv_day_in = findViewById(R.id.main_today_in);
        tv_day_out = findViewById(R.id.main_today_out);
        tv_month_in = findViewById(R.id.main_tv_month_in_money);
        tv_month_out = findViewById(R.id.main_tv_month_out_money);
        today_ls = findViewById(R.id.main_lv);
        tv_month_ye = findViewById(R.id.main_tv_month_ye_money);
    }

//    public void main_add(View view) {
//        Intent intent = new Intent(this, RecordActivity.class);
//        intent.putExtra("type", "add");
//        startActivity(intent);
//    }
//
//    public void main_more(View view) {
//        Intent intent = new Intent(this, MoreActivity.class);
//        startActivity(intent);
//    }
}